package top.itaso.app.ui.demos;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import top.itaso.app.R;
import top.itaso.app.common.SPUtils;

/**
 * @Description: DemosFragment专用适配器，用于展示 Demo 示例列表
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2021/11/30
 */
public class DemoItemAdapter extends RecyclerView.Adapter<DemoItemAdapter.ItemViewHolder> {
    private static final String TAG = "DemoItemAdapter";
    private ArrayList<DemoItemDTO> itemDTOS;

    private IDemoItemClickLister itemClickLister;

    interface IDemoItemClickLister {
        /**
         * Demo点击事件
         *
         * @param view
         * @param position
         * @param itemDTO
         */
        void onItemClick(View view, int position, DemoItemDTO itemDTO);
    }

    public void refreshDemoBeans(ArrayList<DemoItemDTO> itemDTOS) {
        this.itemDTOS = itemDTOS;
        notifyDataSetChanged();
    }

    public void setOnItemClickLister(IDemoItemClickLister itemClickLister) {
        this.itemClickLister = itemClickLister;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_demo_item, parent, false);
        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        DemoItemDTO demoItemDTO = itemDTOS.get(position);

        holder.itemAuthor.setText(demoItemDTO.getAuthor());
        holder.itemTitle.setText(demoItemDTO.getEKey().getTitle());
        holder.itemDesc.setText(demoItemDTO.getEKey().getDesc());
        holder.itemQa.setText(demoItemDTO.getQa());
        holder.itemDate.setText(demoItemDTO.getCreateTime());
        holder.itemTop.setVisibility(demoItemDTO.isShowTop() ? View.VISIBLE : View.GONE);

        if (demoItemDTO.isPrivateModule()) {//是受保护功能时需要判断开关
            boolean isProtected = SPUtils.getInstance().readIsProtect();
            holder.itemView.setEnabled(!isProtected);
        } else {
            holder.itemView.setEnabled(true);
        }
        holder.itemView.setFocusable(holder.itemView.isEnabled());//Item 不可点击时同样不能聚焦选择

        holder.itemView.setSelected(getItemCount() > 0 && position == 0);
        holder.itemLayoutSelectorStatus.setVisibility(SPUtils.getInstance().readIsDebug() ? View.VISIBLE : View.GONE);
        if (holder.itemLayoutSelectorStatus.getVisibility() == View.VISIBLE) {
            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    final String tipStatus = view.getContext().getString(R.string.tip_selector_status, "focused", hasFocus + "");
//                    ALog.dTag(TAG, "%s tipStatus:%s", holder.itemTitle.getText().toString(), tipStatus);
                    holder.itemSelectorFocused.setText(tipStatus);
                }
            });
            holder.itemView.setOnHoverListener(new View.OnHoverListener() {
                @Override
                public boolean onHover(View view, MotionEvent motionEvent) {
                    int action = motionEvent.getAction();
//                    ALog.dTag(TAG, "%s tipStatus::%s", holder.itemTitle.getText().toString(), action);
                    if (motionEvent.getAction() != MotionEvent.ACTION_HOVER_ENTER && motionEvent.getAction() != MotionEvent.ACTION_HOVER_EXIT) {
                        return false;
                    }
                    final String tipStatus = view.getContext().getString(R.string.tip_selector_status, "hovered", action == MotionEvent.ACTION_HOVER_ENTER ? "enter" : "exit");
//                    ALog.dTag(TAG, "%s tipStatus:%s", holder.itemTitle.getText().toString(), tipStatus);
                    switch (action) {
                        case MotionEvent.ACTION_HOVER_ENTER:
                            break;
                        case MotionEvent.ACTION_HOVER_EXIT:
                            break;
                        default:
                            break;
                    }

                    holder.itemSelectorHovered.setText(tipStatus);
                    return false;
                }
            });
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() != MotionEvent.ACTION_DOWN && motionEvent.getAction() != MotionEvent.ACTION_UP) {
                        return false;
                    }
                    final String tipStatus = view.getContext().getString(R.string.tip_selector_status, "pressed", motionEvent.getAction() == MotionEvent.ACTION_DOWN ? "down" : "up");
//                    ALog.dTag(TAG, "%s tipStatus:%s", holder.itemTitle.getText().toString(), tipStatus);
                    holder.itemSelectorPressed.setText(tipStatus);
                    return false;
                }
            });
            final String tipStatus = holder.itemSelectorSelected.getContext().getString(R.string.tip_selector_status, "selected", holder.itemSelectorSelected.isSelected() ? "true" : "false");
            holder.itemSelectorSelected.setText(tipStatus);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickLister != null) {
                    itemClickLister.onItemClick(v, position, demoItemDTO);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (itemDTOS == null || itemDTOS.isEmpty()) {
            return 0;
        }
        return itemDTOS.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        /**
         * Demo提供者，如 itaso
         */
        public TextView itemAuthor;
        /**
         * Demo标题
         */
        public TextView itemTitle;
        /**
         * Demo简介
         */
        public TextView itemDesc;
        /**
         * Demo的发布时间
         */
        public TextView itemDate;
        /**
         * Demo的解释者
         */
        public TextView itemQa;
        /**
         * Demo是否置顶
         */
        public TextView itemTop;

        public View itemLayoutSelectorStatus;
        public TextView itemSelectorFocused;
        public TextView itemSelectorHovered;
        public TextView itemSelectorPressed;
        public TextView itemSelectorSelected;

        public ItemViewHolder(View itemView) {
            super(itemView);
            itemAuthor = itemView.findViewById(R.id.item_author);
            itemTitle = itemView.findViewById(R.id.item_title);
            itemDesc = itemView.findViewById(R.id.item_desc);
            itemDate = itemView.findViewById(R.id.item_date);
            itemQa = itemView.findViewById(R.id.item_qa);
            itemTop = itemView.findViewById(R.id.item_top);
            itemLayoutSelectorStatus = itemView.findViewById(R.id.layout_selector_status);
            itemSelectorFocused = itemView.findViewById(R.id.tv_status_focused);
            itemSelectorHovered = itemView.findViewById(R.id.tv_status_hovered);
            itemSelectorPressed = itemView.findViewById(R.id.tv_status_pressed);
            itemSelectorSelected = itemView.findViewById(R.id.tv_status_selected);
        }
    }
}
